import requests
from bs4 import BeautifulSoup
import os
import re

def download_and_save_image(img_url, img_dir):
    """下载图片并保存到指定目录"""
    headers = {
        'User-Agent': 'Mozilla/5.0'
    }

    # 确保目录存在
    os.makedirs(img_dir, exist_ok=True)
    full_img_url = ""

    if img_url.startswith('/zgwzcy'):
        # 补全图片链接
        full_img_url = 'http://www.cmst.com.cn' + img_url
    elif img_url.startswith('/cctgroup'):
        # 补全图片链接
        full_img_url = 'http://www.cmst.com.cn' + img_url
    elif img_url.startswith('/admin/upload'):
            # 补全图片链接
            full_img_url = 'http://www.cmst.com.cn' + img_url
    else:
        full_img_url = img_url

    try:
        response = requests.get(full_img_url, headers=headers, timeout=10)
        if response.status_code == 200:
            # 获取文件名
            filename = os.path.join(img_dir, os.path.basename(img_url))
            with open(filename, 'wb') as f:
                f.write(response.content)
            print(f"图片已下载: {filename}")
            return filename  # 返回本地路径，供HTML引用
        else:
            print(f"图片下载失败: {full_img_url}，状态码: {response.status_code}")
    except Exception as e:
        print(f"下载图片失败: {full_img_url}，错误: {e}")
    return None

def sanitize_filename(filename):
    # 替换Windows不支持的非法字符
    invalid_chars = r'[\\/:*?"<>|]'
    sanitized = re.sub(invalid_chars, '-', filename)
    return sanitized.strip()

def fetch_page_content(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }

    try:
        response = requests.get(url, headers=headers, timeout=10)
        response.encoding = 'utf-8'  # 根据网页实际编码调整
        if response.status_code == 200:
            return response.text
        else:
            print(f"请求失败，状态码: {response.status_code}")
            return None
    except requests.RequestException as e:
        print(f"请求过程中发生错误: {e}")
        return None